from django.db import models
# Create your models here.
class Resource(models.Model):
    name = models.CharField(max_length=200,verbose_name='资源名称')
    pid = models.ForeignKey('self',null=True,blank=True,verbose_name='父级资源',
                            on_delete=models.SET_NULL,related_name='self_pid',db_constraint=False)
    url = models.CharField(max_length=200,verbose_name='资源路径',null=True,blank=True)
    promition = models.IntegerField(null=True,blank=True,verbose_name='权限')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '资源表'
        db_table = 'resource'
        verbose_name_plural = verbose_name


class Role(models.Model):
    name = models.CharField(max_length=100,verbose_name='角色名称')
    pid = models.ForeignKey('self',null=True,blank=True,verbose_name='父级角色',
                            on_delete=models.SET_NULL,related_name='self_pid',db_constraint=False)
    promition = models.IntegerField(null=True, blank=True, verbose_name='权限')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '角色表'
        db_table = 'role'
        verbose_name_plural = verbose_name


class RoleResource(models.Model):
    role = models.ForeignKey(Role,on_delete=models.CASCADE,db_constraint=False,related_name='rs_role',verbose_name='角色')
    resource = models.ForeignKey(Resource,on_delete=models.CASCADE,db_constraint=False,related_name='rs_source',verbose_name='资源')

    def __str__(self):
        return f"{self.role}-{self.resource}"

    class Meta:
        verbose_name = '角色资源表'
        db_table = 'role_resource'
        verbose_name_plural = verbose_name

class DepartmentModel(models.Model):
    name = models.CharField(max_length=100,unique=True,verbose_name='部门名称')


    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '部门表'
        verbose_name_plural = verbose_name



class PositionModel(models.Model):
    name = models.CharField(max_length=100,verbose_name='职位名称')


    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '职位表'
        verbose_name_plural = verbose_name





class UserModel(models.Model):
    username = models.CharField(max_length=50,verbose_name='用户名')
    phone = models.CharField(max_length=11,verbose_name='手机号')
    password = models.CharField(max_length=32,verbose_name='密码')
    email = models.EmailField(verbose_name='邮箱',null=True,blank=True)
    role = models.ForeignKey(Role,on_delete=models.SET_NULL,db_constraint=False,null=True,blank=True,
                              related_name='user_role',verbose_name='角色')
    department = models.ForeignKey(DepartmentModel,on_delete=models.SET_NULL,db_constraint=False,null=True,blank=True,
                              related_name='user_department',verbose_name='部门')
    position = models.ForeignKey(PositionModel,on_delete=models.SET_NULL,db_constraint=False,null=True,blank=True,
                              related_name='user_position',verbose_name='职位')

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '用户表'
        db_table = 'user'
        verbose_name_plural = verbose_name



class InterfacePromition(models.Model):
    interface = models.CharField(max_length=200,verbose_name='接口路径')
    resource = models.ForeignKey(Resource,on_delete=models.CASCADE,db_constraint=False,
                                 related_name='interface_resource',verbose_name='资源')


    def __str__(self):
        return self.interface


    class Meta:
        verbose_name = '接口权限表'
        db_table = 'interface_promition'
        verbose_name_plural = verbose_name




class Exclusion(models.Model):
    res1 = models.ForeignKey(Resource,on_delete=models.CASCADE,
                             db_constraint=False,related_name="res1_resource",verbose_name="资源1")
    res2 = models.ForeignKey(Resource,on_delete=models.CASCADE,
                             db_constraint=False,related_name="res2_resource",verbose_name="资源2")

    def __str__(self):
        return f"{self.res1.name}-{self.res2.name}"


    class Meta:
        verbose_name = "排除关系表"
        db_table = "exclusion"
        verbose_name_plural = verbose_name



class WorkFlowModel(models.Model):
    STATUS_CHOICES = (
        (1,'可用'),
        (2,'停用'),
    )
    name = models.CharField(max_length=100,verbose_name='流程名称')
    params =models.TextField(verbose_name='流程参数')
    status = models.SmallIntegerField(choices=STATUS_CHOICES,default=2,verbose_name='状态')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '工作流表'
        verbose_name_plural = verbose_name
